package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.QualityAttachment;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.dao.entity.QualityAttachmentDao;
import cc.comac.mom.dao.entity.QualityAttachmentGroupDao;
import cc.comac.mom.entity.QualityAttachmentEntity;
import cc.comac.mom.entity.QualityAttachmentGroupEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class QualityAttachmentGroupDataHandler extends ServiceImpl<QualityAttachmentGroupDao, QualityAttachmentGroupEntity>{

	@Autowired
	private QualityAttachmentGroupDao qualityAttachmentGroupDao;
	
	@Autowired
	private QualityAttachmentDao qualityAttachmentDao;
	
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	public ResultBean insertBatch(List<QualityAttachmentGroup> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		ArrayList<QualityAttachmentGroupEntity> listEntity = new ArrayList<QualityAttachmentGroupEntity>();
		for(QualityAttachmentGroup item :list) {
			QualityAttachmentGroupEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");	
			}
			if(innerData.getAttachmentId() == 0) {
				throw new RuntimeException("附件id不存在");
			}
			if(innerData.getGroupId() == 0) {
				throw new RuntimeException("附件组id不存在");
			}
			baseEntityDataUtil.setBaseEntityData(innerData);
			listEntity.add(innerData);
		}
		boolean saveBatch = this.saveBatch(listEntity);
		if(!saveBatch) {
			throw new RuntimeException("附件组保存失败");
		}
		return resultBean;
	}
	
	public List<QualityAttachmentGroup> getByAttachmentGroupId(long groupId){
		HashMap<String,Object> map = new HashMap<String, Object>();
		map.put("row_status", 1);
		map.put("group_id", groupId);
		List<QualityAttachmentGroupEntity> selectByMap = qualityAttachmentGroupDao.selectByMap(map);
		if(selectByMap == null || selectByMap.size()<1) {
			return null;
		}
		ArrayList<QualityAttachmentGroup> list = new ArrayList<QualityAttachmentGroup>();
		for(QualityAttachmentGroupEntity item :selectByMap) {
			QualityAttachmentGroup qualityAttachmentGroup = new QualityAttachmentGroup();
			qualityAttachmentGroup.setInnerData(item);
			QualityAttachmentEntity attachmentEntity = qualityAttachmentDao.selectById(item.getAttachmentId());
			if(attachmentEntity != null) {
				QualityAttachment qualityAttachment = new QualityAttachment();
				qualityAttachment.setInnerData(attachmentEntity);
				qualityAttachmentGroup.setQualityAttachment(qualityAttachment);
			}
			list.add(qualityAttachmentGroup);
		}
		return list;
		
	}
}
